package com.uu.look.web.filter;



import com.uu.look.common.core.CommonConstants;
import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by liuke on 2016/7/25.
 */

//@WebFilter(filterName="sessionFilter",urlPatterns="/*")
@Component
public class SessionFilter implements Filter {
    /**
     * 需要排除的页面
     */
//    private String excludedPages;
//
//    private String[] excludedPageArray;

    /**
     * 封装，不需要过滤的list列表
     */
    protected static List<String> excludeList = new ArrayList<String>();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
//        excludedPages = filterConfig.getInitParameter("excludedPages");
//        if (!StringUtil.isEmpty(excludedPages)) {
//            excludedPageArray = excludedPages.split(",");
//        }
        excludeList.add("index.html");
        excludeList.add("html/login.html");
        excludeList.add("randomCode");
        excludeList.add("js");
        excludeList.add("css");
        excludeList.add("jpg");
        excludeList.add("png");
        excludeList.add("user/login");
        excludeList.add("ico");
        excludeList.add("html/undefined");
        return;
    }
    /**
     * 是否需要过滤
     * @param url
     * @return
     */
    private boolean isInclude(String url) {
        for (String exclude : excludeList) {
            if(url.indexOf(exclude)>-1||"/".equals(url)){
                return true;
            }
        }
        return false;
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        String url = httpRequest.getRequestURI().substring(httpRequest.getContextPath().length());
        if (url.startsWith("/") && url.length() > 1) {
            url = url.substring(1);
        }

        if (isInclude(url)){
            filterChain.doFilter(httpRequest, httpResponse);
        } else {
            HttpSession session = httpRequest.getSession();
            if (session == null || session.getAttribute(CommonConstants.SESSION_USER) == null) {
                String type = httpRequest.getHeader("X-Requested-With");
                if ("XMLHttpRequest".equalsIgnoreCase(type)) {// AJAX REQUEST PROCESS
                    HttpServletResponse response = (HttpServletResponse)servletResponse;
                    response.setHeader("sessionStatus", "timeout");
                    response.setHeader("path", httpRequest.getContextPath());
                }else{
                    ((HttpServletResponse) servletResponse).sendRedirect(httpRequest.getContextPath()+"/");
                }
                return;
            } else {
                filterChain.doFilter(httpRequest, servletResponse);
            }
        }
    }
    @Override
    public void destroy() {
        return;
    }

}
